package com.jiutong.meipenggang.http.present;

import android.content.Context;


import com.jiutong.meipenggang.entity.BaseBean;
import com.jiutong.meipenggang.entity.LoginBean;
import com.jiutong.meipenggang.entity.RegisterBean;
import com.jiutong.meipenggang.http.model.interfaces.RegisterAndLoginBiz;
import com.jiutong.meipenggang.http.model.requests.UserInfoRequest;
import com.jiutong.meipenggang.http.subscribers.ProgressSubscriber;
import com.jiutong.meipenggang.http.subscribers.SubscriberOnNextListener;
import com.jiutong.meipenggang.utils.EncryptUtil;

import java.util.HashMap;



/**
 * @author Veken
 */
public class UserInfoPresent {

    private  Context mContext;
    private  RegisterAndLoginBiz mRegisterBiz;
    private  UserInfoRequest userInfoRequest;
//    private final String registrationID;

    public UserInfoPresent(Context context, RegisterAndLoginBiz registerBiz) {
        this.mContext = context;
        this.mRegisterBiz = registerBiz;
        userInfoRequest = new UserInfoRequest();
//        registrationID = JPushInterface.getRegistrationID(mContext);
    }

    /**
     * 注册
     */
    public void register() {

        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<RegisterBean>() {
            @Override
            public void onNext(RegisterBean subjects) {
                mRegisterBiz.onSuccess(subjects);
            }

            @Override
            public void onError(Throwable e) {
                mRegisterBiz.onError(e);
            }

        };

        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("OPT",mRegisterBiz.getOpt());
        fieldsMap.put("cellPhone", EncryptUtil.encrypt3DES(mRegisterBiz.getPhoneNum()));
        fieldsMap.put("password", EncryptUtil.encrypt3DES(mRegisterBiz.getOriPwd()));
        fieldsMap.put("confirmPassword",EncryptUtil.encrypt3DES(mRegisterBiz.getNewPwd()));
        fieldsMap.put("phoneCode",mRegisterBiz.getVerificatCode());


        userInfoRequest.registed(new ProgressSubscriber(onNextListener, mContext), fieldsMap);
    }

    /**
     * 登录
     */
    public void login() {

        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<LoginBean>() {
            @Override
            public void onNext(LoginBean subjects) {
                mRegisterBiz.onSuccess(subjects);
            }

            @Override
            public void onError(Throwable e) {
                mRegisterBiz.onError(e);
            }
        };

        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("OPT",mRegisterBiz.getOpt());
        fieldsMap.put("cellPhone", EncryptUtil.encrypt3DES(mRegisterBiz.getPhoneNum()));
        fieldsMap.put("password", EncryptUtil.encrypt3DES(mRegisterBiz.getOriPwd()));

        userInfoRequest.login(new ProgressSubscriber(onNextListener, mContext), fieldsMap);
    }


    /**
     * 校验验证码
     */
    public void checkPhoneCode() {

        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<BaseBean>() {
            @Override
            public void onNext(BaseBean subjects) {
                mRegisterBiz.onSuccess(subjects);
            }

            @Override
            public void onError(Throwable e) {
                mRegisterBiz.onError(e);
            }
        };

        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("OPT",mRegisterBiz.getOpt());
        fieldsMap.put("cellPhone",EncryptUtil.encrypt3DES(mRegisterBiz.getPhoneNum()));
        fieldsMap.put("phoneCode", mRegisterBiz.getVerificatCode());

        userInfoRequest.pwdResetByCode(new ProgressSubscriber(onNextListener, mContext), fieldsMap);
    }

    /**
     * 注册获取验证码
     */
    public void getAuthCode() {

        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<BaseBean>() {
            @Override
            public void onNext(BaseBean subjects) {
                mRegisterBiz.onVerCodeGetSuccess(subjects);
            }

            @Override
            public void onError(Throwable e) {
                mRegisterBiz.onError(e);
            }
        };

        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("OPT", mRegisterBiz.getOpt());
        fieldsMap.put("cellPhone", EncryptUtil.encrypt3DES(mRegisterBiz.getPhoneNum()));
        userInfoRequest.getAuthCode(new ProgressSubscriber(onNextListener, mContext), fieldsMap);
    }

    /**
     * 忘记密码获取验证码
     */
    public void getAuthCodeByFoget() {

        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<BaseBean>() {
            @Override
            public void onNext(BaseBean subjects) {
                mRegisterBiz.onVerCodeGetSuccess(subjects);
            }

            @Override
            public void onError(Throwable e) {
                mRegisterBiz.onError(e);
            }
        };

        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("OPT",5);
        fieldsMap.put("cellPhone", EncryptUtil.encrypt3DES(mRegisterBiz.getPhoneNum()));
        userInfoRequest.getAuthCode(new ProgressSubscriber(onNextListener, mContext), fieldsMap);
    }


    /**
     * 重置密码
     */
    public void forgetPwd() {

        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<BaseBean>() {
            @Override
            public void onNext(BaseBean subjects) {
                mRegisterBiz.onSuccess(subjects);
            }

            @Override
            public void onError(Throwable e) {
                mRegisterBiz.onError(e);
            }
        };

        HashMap<String, Object> fieldsMap = new HashMap<>();
        fieldsMap.put("OPT",mRegisterBiz.getOpt());
        fieldsMap.put("cellPhone", EncryptUtil.encrypt3DES(mRegisterBiz.getPhoneNum()));
        fieldsMap.put("password", EncryptUtil.encrypt3DES(mRegisterBiz.getOriPwd()));
        fieldsMap.put("confirmPassword",EncryptUtil.encrypt3DES(mRegisterBiz.getNewPwd()));

        userInfoRequest.forgetPwd(new ProgressSubscriber(onNextListener, mContext), fieldsMap);
    }




//    /**
//     * 关于我们
//     */
//    public void aboutUs() {
//
//        SubscriberOnNextListener onNextListener = new SubscriberOnNextListener<BaseRespond>() {
//            @Override
//            public void onNext(BaseRespond subjects) {
//                mRegisterBiz.onSuccess(subjects);
//            }
//
//            @Override
//            public void onError(Throwable e) {
//                mRegisterBiz.onError(e);
//            }
//        };
//
//        HashMap<String, Object> fieldsMap = new HashMap<>();
//
//        userInfoRequest.aboutUs(new ProgressSubscriber(onNextListener, mContext), fieldsMap);
//    }



}
